package com.shadow.bus.main.history;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.shadow.bus.entity.HistoryEntity;
import com.shadow.bus.entity.OperationEntity;
import com.shadow.bus.entity.PartEntity;
import com.shadow.bus.entity.PersonEntity;
import com.shadow.bus.entity.RepairEntity;
import com.shadow.bus.entity.TroubleEntity;
import com.shadow.bus.entity.TroubleOperationEntity;
import com.tepth.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhangyu
 * @date 2018/7/9
 */

public class HistoryDataConverter {

    public void convert(final String response, final OnConverterListener listener) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                ArrayList<HistoryEntity> historyEntities = new ObjConverter<ArrayList<HistoryEntity>>().
                        convert(response, new TypeReference<ArrayList<HistoryEntity>>() {
                        });
                ArrayList<MultiItemEntity> res = new ArrayList<>();
                if (historyEntities != null) {
                    for (HistoryEntity b : historyEntities) {
                        Level0Item level0 = new Level0Item(b.getActualStartTime(), b.getMaintenanceTypeName());
                        if (b.getType() == 2 || b.getType() == 1) {
                            //小修
                            RepairEntity repair = b.getRepair();
                            if (repair != null) {
                                ArrayList<TroubleEntity> troubles = repair.getListTrouble();
                                if (troubles != null && !troubles.isEmpty()) {
                                    for (TroubleEntity t : troubles) {
                                        String troubleDes = t.getName();
                                        if (troubleDes != null && troubleDes.contains("自定义录入@@")) {
                                            troubleDes = troubleDes.replaceAll("自定义录入@@", "附加作业项");
                                        }
                                        String fixContent;
                                        String personName = "";
                                        List<TroubleOperationEntity> contents = t.getListContent();
                                        if (contents != null && !contents.isEmpty()) {
                                            for (TroubleOperationEntity content : contents) {
                                                fixContent = assemblyData(content);
                                                PersonEntity person = content.getWorkPerson();
                                                if (person != null) {
                                                    personName = person.getName();
                                                }
                                                Level1Item level1 = new Level1Item(troubleDes, fixContent, personName);
                                                level0.addSubItem(level1);
                                            }

                                        }

                                    }
                                }
                            }
                        } else {
                            //保养
                            List<TroubleOperationEntity> contents = b.getListRemark();
                            if (contents != null && !contents.isEmpty()) {
                                for (TroubleOperationEntity content : contents) {
                                    String troubleDes = content.getName();
                                    if (troubleDes != null && troubleDes.contains("@@")) {
                                        troubleDes = troubleDes.replaceAll("@@", "");
                                    }
                                    String fixContent = assemblyData(content);
                                    String personName = "";
                                    List<PersonEntity> persons = content.getListWorkPerson();
                                    if (persons != null && !persons.isEmpty()) {
                                        for (PersonEntity person : persons) {
                                            personName += person.getName() + "  ";
                                        }
                                    }
                                    Level1Item level1 = new Level1Item(troubleDes, fixContent, personName);
                                    level0.addSubItem(level1);
                                }
                            }
                        }
                        res.add(level0);
                    }
                }
                e.onNext(res);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onNext(Object o) {
                        ArrayList<MultiItemEntity> list = (ArrayList<MultiItemEntity>) o;
                        if (listener != null) {
                            listener.onConverter(list);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public interface OnConverterListener {
        void onConverter(ArrayList<MultiItemEntity> list);
    }


    private String assemblyData(TroubleOperationEntity bean) {
        String operationContent = "";
        OperationEntity operationBean =
                bean.getOperation();
        if (operationBean != null) {
            operationContent += operationBean.getName();
        }
        PartEntity accessoryBean =
                bean.getAccessory();
        PartEntity componentBean =
                bean.getComponent();
        if (accessoryBean != null) {
            operationContent += accessoryBean.getName();
            operationContent += bean.getUseCount();
            operationContent += accessoryBean.getUnit();
        } else {
            if (componentBean != null) {
                operationContent += componentBean.getName();
                operationContent += bean.getUseCount();
                operationContent += componentBean.getUnit();
            }
        }
        return operationContent;
    }

}
